package com.client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JDialog;

import com.metier.logique.MSG;
import com.metier.persistance.Historique;
import javax.swing.JButton;
import java.awt.BorderLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import org.eclipse.wb.swing.FocusTraversalOnArray;
import java.awt.*;
/**
 * @name DialogSupprHistorique
 * @description Boite de dialog confirmant la suppression de l'historique.
 * @method DialogSupprHistorique - Constructeur par défaut.
 * 
 * @author Adel
 */
public class DialogSupprHistorique extends JDialog implements ActionListener, KeyListener {
	private Historique histPOI;
	private JButton btnOk;
	private JButton btnAnnuler;
	public DialogSupprHistorique() {
		setModal(true);
		setUndecorated(true);
		buildDialog();
	}
	public void buildDialog(){
		setFont(new Font("Dialog", Font.PLAIN, 12));
		setTitle("Supprimer l'historique");
		setResizable(false);
		setSize(300, 120);
		setIconImage(Toolkit.getDefaultToolkit().getImage(DialogSupprHistorique.class.getResource("/javax/swing/plaf/metal/icons/Warn.gif")));
		
		btnOk = new JButton("OK");
		btnOk.addActionListener(this);
		
		btnAnnuler = new JButton("Annuler");
		btnAnnuler.addActionListener(this);
		
		JLabel lblEtesvousSreDe = new JLabel("Etes-vous sûre de vouloir supprimer l'historique?");
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(50)
							.addComponent(btnOk)
							.addGap(51)
							.addComponent(btnAnnuler))
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap()
							.addComponent(lblEtesvousSreDe)))
					.addContainerGap(34, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(28)
					.addComponent(lblEtesvousSreDe)
					.addGap(27)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnOk)
						.addComponent(btnAnnuler))
					.addContainerGap())
		);
		getContentPane().setLayout(groupLayout);
		getContentPane().setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{btnAnnuler, btnOk}));
		centerScreen();
	}
	public void centerScreen() {
		  Dimension dim = getToolkit().getScreenSize();
		  Rectangle abounds = getBounds();
		  setLocation((dim.width - abounds.width) / 2,
		      (dim.height - abounds.height) / 2);
		  requestFocus();
		}
	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		Object source = e.getSource();
		if(source == btnOk){
			
			// On supprime l'historique en BDD.
			MSG oMSG = new MSG();
			histPOI = new Historique();

			oMSG.setData("method", "deleteAll");
			histPOI.delete(oMSG);
			
			this.setVisible(false);
		}
		else if(source == btnAnnuler){
			this.setVisible(false);
		}

	
		
	}
}
